<?php
///////////////////////////////////////////////////////////////////////////

require_once 'lib/hashed_array.php';

///////////////////////////////////////////////////////////////////////////

class SiolEpg
{
	private $http_opts = null;
	
	public function __construct()
	{
		hd_print('SiolEpg::__construct invoked.');
		$this->http_opts = array (
		CURLOPT_FOLLOWLOCATION  => true,
		CURLOPT_MAXREDIRS       => 10,
// debug            CURLOPT_PROXY           => '192.168.1.9:3128',
		CURLOPT_COOKIEFILE      => '/tmp/siol.cookies',
		CURLOPT_COOKIEJAR       => '/tmp/siol.cookies'
        );
		
		
	//TODO: load mapping
	}
	
	public function GetEpg($url, $seconds, $wanted_day, $channel, $show_details)
	{
		hd_print("GetEpg invoked with $url");
		$epg_items = array();
		$programs = $this->GetPrograms($channel, $wanted_day);
		$end_day = false;
		foreach ($programs as $program)
		{
			$epg_item = new SiolEpgItem();
			$epg_item->Name = $program->Name;
			if ($show_details == 'yes')
			{
				$details = $this->GetDetails($program->Link);
			}
			else
			{
				$details = '';
			}
			$epg_item->Description = $details;
			
			$numbers = explode(":", $program->Time);
			$program_seconds = intval($numbers[0]) * 3600 + intval($numbers[1]) * 60;
			if (intval($numbers[0]) < 5 && $end_day == true)
			{
				break;
			}
			if (intval($numbers[0]) > 20)
			{
				$end_day = true;
			}
			
			$epg_item->StartTime = intval($seconds) + intval($program_seconds);
			//hd_print("Adding program: $program->Name, original time: $program->Time, seconds: $program_seconds");
			array_push($epg_items, $epg_item);
		}
		return $epg_items;
	}
	
	public function GetChannelStr($channel_id)
	{
		hd_print("GetChannelStr invoked with $channel_id");
		//return 'TV+SLO+1';
		return $this->GetChannel($channel_id)->Name;
	}
	
	//return array of SiolProgram
	 private function GetPrograms($channel, $wanted_day)
	 {
	   //echo 'GetPrograms channel: ' . $channel;
	   $root_url = 'http://www.siol.net/tv-spored.aspx';
	   $url = $root_url . '?ch=' . $channel . '&p1=' . $wanted_day . '&p3=0&p4=0';
	   //echo 'GetPrograms url: ' . $url;
	   $html = file_get_contents($url);

	   $pos1 = strpos($html, '<div class="hr"></div><div class="lst"><div class="slc"><div class="def">') + 39;
	   $pos2 = strpos($html, '</div></div></div>', $pos1);

	   $channelsHtml = substr ($html, $pos1, $pos2 - $pos1 + 12);
	   $channelsHtml = str_replace('&', '&amp;', $channelsHtml);
	   
	   //echo $channelsHtml;
	   //echo htmlspecialchars($channelsHtml);
	   $channelsHtml = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . $channelsHtml;
	   $doc = new DOMDocument();
	   $doc->loadHTML($channelsHtml);
	   $divs = $doc->getElementsByTagName('div');
		
		$spored = array();
		
		foreach ($divs as $value)
		{
		  $class = $value->attributes->getNamedItem('class')->nodeValue;
		  if ($class == 'def' || $class == 'def alt')
		  {
			$siolProgram = new SiolProgramItem();
			foreach ($value->childNodes as $child)
			{ 
			  $childclass = $child->attributes->getNamedItem('class')->nodeValue;
			  if ($childclass == 'i3' || $childclass == 'i3 ovr' || $childclass == 'i3 now')
			  {
				$siolProgram->Time = $child->nodeValue;
			  }        
			  if ($childclass == 'i5' || $childclass == 'i5 ovr' || $childclass == 'i5 now')
			  {
				$siolProgram->Name = $child->nodeValue;
				
				$siolProgram->Link = $root_url . $child->firstChild->attributes->getNamedItem('href')->nodeValue;
				}

			}
			array_push($spored, $siolProgram);
		  }
		}
	   return $spored;   
	}
	
	//return SiolProgramDescription
	function GetDetails($url)
	{
	  hd_print("GetDetails invoked with $url");
	  $html2 = file_get_contents($url);
	  
	  //echo 'details url: ' . $url;
	  $fpos1 = strpos($html2, '<p class="zanr">');
	  $fpos2 = strpos($html2, '</p><div class="clrA"></div>', $fpos1);

	  $fchannelsHtml = substr ($html2, $fpos1, $fpos2 - $fpos1 + 4);
	  $fchannelsHtml = str_replace('<p>&nbsp;</p>', '', $fchannelsHtml);
	  $fchannelsHtml = preg_replace('@<script[^>]*?>.*?</script>@si', "", $fchannelsHtml);
	  
	  //echo htmlspecialchars($fchannelsHtml);
	  $fchannelsHtml = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . $fchannelsHtml;
	  $doc2 = new DOMDocument();
	  $doc2->loadHTML($fchannelsHtml);
	  $divs2 = $doc2->getElementsByTagName('p');  

	  $str = '';
	  $counter = 1;
	  $details = new SiolProgramDescription();
	  foreach ($divs2 as $subobj)
	  {
		if ($counter == 1)
		{
		  //Zanr:
		  $details->Genre = $subobj->nodeValue;
		}
		else if ($counter == 2)
		{
		  //Opis:
		  $details->Description = $subobj->nodeValue;
		}
		else
		{
		  $str = $str . $subobj->nodeValue;
		}
		$counter++;
	  }
	  $details->Other = $str;
	  return $details;
	}
	
	 /// return SiolEpgChannel
	function GetChannel($channelName)
	{
	  $channelsUrl = 'http://www.siol.net/tv-spored.aspx';
	  $channelsHtml = file_get_contents($channelsUrl);
	  
	  $fpos1 = strpos($channelsHtml, '<aside class="lnk_arrow lnk1">') + 30;
	  $fpos2 = strpos($channelsHtml, '</aside><div class="clrA">', $fpos1);  
	  $subChannelsHtml = substr ($channelsHtml, $fpos1, $fpos2 - $fpos1);
	  $subChannelsHtml = str_replace('<aside class="lnk_arrow lnk2">', '', $subChannelsHtml);
	  $subChannelsHtml = str_replace('<div class="hr"></div>', '', $subChannelsHtml);
	  $subChannelsHtml = str_replace('</aside>', '', $subChannelsHtml);
	  $subChannelsHtml = str_replace('&', '&amp;', $subChannelsHtml);
	  $subChannelsHtml = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . $subChannelsHtml;
	  
	 $doc2 = new DOMDocument();
	 $doc2->loadHTML($subChannelsHtml);
	 $channelLinks = $doc2->getElementsByTagName('a');  
	 
	 foreach ($channelLinks as $channelLink)
	 {
		$title = $channelLink->attributes->getNamedItem('title')->nodeValue;
		$href = $channelLink->attributes->getNamedItem('href')->nodeValue;
		$name = $channelLink->nodeValue;
		if ($channelName == $name)
		{
		  $channel = new SiolEpgChannel();
		  $channel->Name = $title;
		  $channel->DisplayName = $name;
		  $channel->Link = $href;
		  return $channel;
		}
	 } 
	}
}

class SiolEpgItem
{
	public $Name;
	public $Description;
	public $StartTime;
}

class SiolProgramItem
{
	public $Time;
	public $Name;
	public $Link;
}
 

class SiolEpgChannel
{
  public $Name;
  public $Link;
  public $DisplayName;
}

class SiolProgramDescription
{
  public $Genre;
  public $Description;
  public $Other;
}

///////////////////////////////////////////////////////////////////////////
?>